﻿//-----------------------------------------------------------------------
// <copyright file="StringExtensions.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: StringExtensions.cs
// * history : created by qinchaoyue 2017-10-16 10:06:13
// </copyright>
//-----------------------------------------------------------------------

namespace Slwy.Utility.Extensions
{
    using System;
    using System.Globalization;
    using System.Security.Cryptography;
    using System.Text;

    /// <summary>
    /// String 扩展方法类
    /// 一些常用的方法
    /// </summary>
    public static class StringExtensions
    {
        /// <summary>
        /// 指示指定的字符串是 null 还是 System.String.Empty 字符串
        /// </summary>
        /// <param name="value"> The value.  </param>
        /// <returns> The <see cref="bool"/>.  </returns>
        public static bool IsNullOrEmpty(this string value)
        {
            return string.IsNullOrEmpty(value);
        }

        /// <summary>
        /// 指定的字符串是否是数字类型
        /// </summary>
        /// <param name="value">the value</param>
        /// <returns>true of false</returns>
        public static bool IsNumber(this string value)
        {
            try
            {
                Int16.Parse(value);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 为指定格式的字符串填充相应对象来生成字符串
        /// </summary>
        /// <param name="format">字符串格式，占位符以{n}表示</param>
        /// <param name="args">用于填充占位符的参数</param>
        /// <returns>格式化后的字符串</returns>
        public static string FormatWith(this string format, params object[] args)
        {
            format.CheckNotNull("format");
            return string.Format(CultureInfo.CurrentCulture, format, args);
        }

        /// <summary>
        /// 获取字符串的 md5 
        /// </summary>
        /// <param name="value"> 字符串 </param>
        /// <returns> MD5 后的字符串<see cref="string"/>.  </returns>
        public static string GetMD5(this string value)
        {
            using (var md5 = MD5.Create())
            {
                return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(value))).Replace("-", string.Empty);
            }
        }
    }
}
